import Cookies from 'js-cookie'

/**
 * 默认过期时间
 */
const DEFAULT_EXPIRES = 1

enum KEYS {
    SITE_CONFIG = 'siteConfig',
    TOKEN = 'token',
    // 马兰花长token
    MLHTOKEN = 'mlhToken',
    PLATFORMTOKEN = 'planformToken',
    TEACHERTOKEN = 'teacherToken',
}

/**
 * 获取 cookie
 * @param key
 * @returns
 */
export const getCookie = (key: keyof typeof KEYS) => {
    // if (typeof window === 'undefined') return
    const res = Cookies.get(KEYS[key]) as any

    try {
        return JSON.parse(res)
    } catch (e) {
        return res
    }
}

/**
 * 写入 cookie
 * @param key
 * @param data
 * @param expires
 */
export const setCookie = (key: keyof typeof KEYS, data: any, expires: number = DEFAULT_EXPIRES) => {
    // if (!window) return
    Cookies.set(KEYS[key], typeof data === 'object' ? JSON.stringify(data) : data, {
        expires,
        // domain: 'wozhipei.com',
    })
}

/**
 * 删除
 * @param key
 */
export const delCookie = (key: keyof typeof KEYS) => {
    // if (!window) return
    Cookies.remove(KEYS[key])
}
